package com.atguigu.tingshu.order.strategy;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderDerateService;
import com.atguigu.tingshu.order.service.OrderDetailService;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 保存订单数据的策略接口
 */
public interface SaveOrderStrategy {

    // 保存订单算法规则
    public abstract OrderInfoVo saveOrder(OrderInfoVo orderInfoVo) ;

    // 获取支付方式
    public abstract String getPayway() ;

    // 保存订单基本数据(order_info)、保存订单的明细数据（order_detail）、保存订单的减免明细(order_derate)
    public default void saveOrderInfoAndOrderDetailAndOrderDerate(OrderInfoVo orderInfoVo ,
                                                                  OrderInfoService orderInfoService ,
                                                                  OrderDetailService orderDetailService ,
                                                                  OrderDerateService orderDerateService
    ) {

        // 获取当前登录用户的id
        Long userId = AuthContextHolder.getUserId();

        // 获取订单明细列表
        List<OrderDetailVo> orderDetailVoList = orderInfoVo.getOrderDetailVoList();

        // 保存订单的基本数据
        OrderInfo orderInfo = new OrderInfo() ;
        orderInfo.setUserId(userId);
        OrderDetailVo orderDetailVo = orderDetailVoList.get(0);
        orderInfo.setOrderTitle(orderDetailVo.getItemName());
        orderInfo.setOrderNo(orderInfoVo.getTradeNo());
        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_UNPAID);       // 未支付
        orderInfo.setOriginalAmount(orderInfoVo.getOriginalAmount());
        orderInfo.setDerateAmount(orderInfoVo.getDerateAmount());
        orderInfo.setOrderAmount(orderInfoVo.getOrderAmount());
        orderInfo.setItemType(orderInfoVo.getItemType());
        orderInfo.setPayWay(orderInfoVo.getPayWay());
        orderInfoService.save(orderInfo) ;             // mybatis plus保存数据，会进行id回传

        // 保存订单明细数据
        List<OrderDetail> orderDetailList = orderDetailVoList.stream().map(odv -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderInfo.getId());
            orderDetail.setItemId(odv.getItemId());
            orderDetail.setItemName(odv.getItemName());
            orderDetail.setItemUrl(odv.getItemUrl());
            orderDetail.setItemPrice(odv.getItemPrice());
            return orderDetail;
        }).collect(Collectors.toList());
        orderDetailService.saveBatch(orderDetailList) ;

        // 保存减免明细数据
        List<OrderDerateVo> orderDerateVoList = orderInfoVo.getOrderDerateVoList();
        if(!CollectionUtils.isEmpty(orderDerateVoList)) {
            List<OrderDerate> orderDerateList = orderDerateVoList.stream().map(orderDerateVo -> {
                OrderDerate orderDerate = new OrderDerate();
                orderDerate.setOrderId(orderInfo.getId());
                orderDerate.setDerateType(orderDerateVo.getDerateType());
                orderDerate.setDerateAmount(orderDerateVo.getDerateAmount());
                orderDerate.setRemarks(orderDerateVo.getRemarks());
                return orderDerate;
            }).collect(Collectors.toList());
            orderDerateService.saveBatch(orderDerateList) ;
        }

        // 调用后续操作的方法
        saveOrderAfterProcess(orderInfoVo) ;

    }

    public abstract void saveOrderAfterProcess(OrderInfoVo orderInfoVo) ;
}
